package com.itbac.controller;

import com.alibaba.fastjson.JSON;
import com.itbac.domain.Order;
import com.itbac.domain.Product;
import com.itbac.domain.User;
import com.itbac.openfeign.order.OrderClient;
import com.itbac.openfeign.product.ProductClient;
import com.itbac.openfeign.user.UserClient;
import com.itbac.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author: BacHe
 * @email: 1218585258@qq.com
 * @Date: 2021/4/19 0:23
 */


@Slf4j
@RestController
public class OrderController implements OrderClient {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Resource
    private ProductClient productClient;

    @Resource
    private UserClient userClient;



    @Override
    public Order createOrder(
            Integer uid,
            Integer pid,
           Integer number) {
        //远程调用
        //查询商品
        //service-product 使用 Ribbon服务发现,负载均衡
        Product product =  productClient.getById(pid);
        log.info("createOrder,查询到的商品信息,{}", JSON.toJSONString(product));
        //查询用户
        User user = userClient.getOne(uid);
        //创建订单
        Order order = new Order();
        order.setUid(user.getUid());
        order.setUsername(user.getUsername());
        order.setPid(product.getPid());
        order.setPname(product.getName());
        order.setPrice(product.getPrice());
        order.setNumber(number);
        return orderService.save(order);
    }
}
